﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetNote.Repository;
using NetNote.ViewModels;

namespace NetNote.Controllers
{
    [Authorize]
    public class NoteController : Controller
    {
        private readonly INoteRepository _noteRepository;
        private readonly INoteTypeRepository _noteTypeRepository;
        public NoteController(INoteRepository noteRepository, INoteTypeRepository noteTypeRepository)
        {
            _noteRepository = noteRepository;
            _noteTypeRepository = noteTypeRepository;

        }
        public async Task<IActionResult> Index(int pageIndex = 1)
        {
            // var note = await _noteRepository.ListAsync();
            // return View(note);
            int pageSize = 5;
            var notes = _noteRepository.PageList(pageIndex, pageSize);
            ViewBag.PageCount = notes.Item2;
            ViewBag.PageIndex = pageIndex;
            return View(notes.Item1);
        }

        public async Task<IActionResult> Add()
        {
            var types = await _noteTypeRepository.ListAsync();
            ViewBag.Types = types.Select(s => new SelectListItem { Text = s.Name, Value = s.Id.ToString() });
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Add([FromServices]IHostingEnvironment env, NoteModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            string filename = string.Empty;
            if (model.Attachment != null)
            {
                filename = Path.Combine("file", Guid.NewGuid().ToString()) +
                           Path.GetExtension(model.Attachment.FileName);
                using (var stream = new FileStream(Path.Combine(env.WebRootPath, filename), FileMode.CreateNew))
                {
                    await model.Attachment.CopyToAsync(stream);
                }
            }
            await _noteRepository.AddAsync(new Models.Note()
            {
                Title = model.Title,
                Content = model.Content,
                Create = DateTime.Now,
                TypeId = model.Type,
                Attachment = filename,
                Password = model.Password
            });
            return RedirectToAction("Index");
        }

        public async Task<IActionResult> Detail(int id)
        {
            var note = await _noteRepository.GetByidAsync(id);
            if (!string.IsNullOrEmpty(note.Password))
            {
                return View();
            }
            if (!string.IsNullOrEmpty(note.Attachment))
            {
                note.Attachment = "https://localhost:44338/" + note.Attachment;
            }
          
            return View(note);
        }
        [HttpPost]
        public async Task<IActionResult> Detail(int id, string password)
        {
            var note = await _noteRepository.GetByidAsync(id);
            if (!note.Password.Equals(password))
            {
                return BadRequest("密码错误,返回从新输入");
            }
            if (!string.IsNullOrEmpty(note.Attachment))
            {
                note.Attachment = "https://localhost:44338/" + note.Attachment;
            }
            return View(note);
        }
    }
}